package com.wty.shop.controller.portal;

import com.wty.shop.common.ResponseCode;
import com.wty.shop.common.ServerResponse;
import com.wty.shop.pojo.User;
import com.wty.shop.service.IOrderService;
import com.wty.shop.util.CookieUtil;
import com.wty.shop.util.JsonUtil;
import com.wty.shop.util.RedisShardedPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * @Author wangtengyu
 * @Create 2018-01-18-12:22
 */
@Controller
@RequestMapping("/order/")
@Slf4j
public class OrderController {


    @Autowired
    private IOrderService iOrderService;

    /**
     * 创建订单
     * @param shippingId
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("create.do")
    @ResponseBody
    public ServerResponse createOrder(Integer shippingId,HttpServletRequest httpServletRequest){
        String loginToken= CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
        }
        String userJsonStr= RedisShardedPoolUtil.get(loginToken);
        User user= JsonUtil.String2Obj(userJsonStr,User.class);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }

        return iOrderService.createOrder(user.getId(),shippingId);
    }

    /**
     * 取消订单
     * @param orderNo
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("cancel.do")
    @ResponseBody
    public ServerResponse cancelOrder(long orderNo,HttpServletRequest httpServletRequest){
        String loginToken=CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
        }
        String userJsonStr= RedisShardedPoolUtil.get(loginToken);
        User user=JsonUtil.String2Obj(userJsonStr,User.class);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }

        return iOrderService.cancelOrder(user.getId(),orderNo);
    }

    /**
     * 获取订单商品信息
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("get_order_cart_product.do")
    @ResponseBody
    public ServerResponse getOrderCartProduct(HttpServletRequest httpServletRequest){
        String loginToken=CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
        }
        String userJsonStr= RedisShardedPoolUtil.get(loginToken);
        User user=JsonUtil.String2Obj(userJsonStr,User.class);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.getOrderCartProduct(user.getId());
    }

    /**
     * 获取订单详情
     * @param httpServletRequest
     * @param orderNo
     * @return
     */
    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse detail(HttpServletRequest httpServletRequest, Long orderNo){
        String loginToken=CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
        }
        String userJsonStr= RedisShardedPoolUtil.get(loginToken);
        User user=JsonUtil.String2Obj(userJsonStr,User.class);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.getOrderDetail(user.getId(),orderNo);
    }

    /**
     * 获取订单列表
     * @param httpServletRequest
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse list(HttpServletRequest httpServletRequest, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        String loginToken=CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isEmpty(loginToken)){
            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
        }
        String userJsonStr= RedisShardedPoolUtil.get(loginToken);
        User user=JsonUtil.String2Obj(userJsonStr,User.class);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.getOrderList(user.getId(),pageNum,pageSize);
    }
















//    /**
//     * 预支付,返回一个二维码给用户扫描
//     * @param httpServletRequest
//     * @param orderNo
//     * @param request
//     * @return
//     */
//    @RequestMapping("pay.do")
//    @ResponseBody
//    public ServerResponse pay(HttpServletRequest httpServletRequest, Long orderNo, HttpServletRequest request){
//        String loginToken=CookieUtil.readLoginToken(httpServletRequest);
//        if(StringUtils.isEmpty(loginToken)){
//            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
//        }
//        String userJsonStr= RedisShardedPoolUtil.get(loginToken);
//        User user=JsonUtil.String2Obj(userJsonStr,User.class);
//        if(user==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
//        }
//        String path=request.getSession().getServletContext().getRealPath("upload");
//        return iOrderService.pay(user.getId(),orderNo,path);
//    }
//
//    /**
//     * 支付宝回掉函数
//     * @param request
//     * @return
//     */
//    @RequestMapping("alipay_callback.do")
//    @ResponseBody
//    public Object alipayCallback(HttpServletRequest request){
//        //接收请求中的参数
//        Map requestParams=request.getParameterMap();
//        //定义一个map，获取处理过后的参数
//        Map<String,String> params= Maps.newHashMap();
//
//        for(Iterator iterator= requestParams.keySet().iterator();iterator.hasNext();){
//            String name= (String) iterator.next();
//            //key对应的value 转成数组
//            String[] values= (String[]) requestParams.get(name);
//            String valuestr="";
//            for(int i=0;i<values.length;i++){
//                //i==values.length-1 最后一个元素
//                valuestr = (i==values.length-1)?valuestr+values[i]:valuestr+values[i]+",";
//            }
//            params.put(name,valuestr);
//        }
//        log.info("支付宝回调,sign:{},trade_status:{},参数:{}",params.get("sign"),params.get("trade_status"),params.toString());
//
//        //非常重要,验证回调的正确性,是不是支付宝发的.并且呢还要避免重复通知.
//
//        params.remove("sign_type");
//
//        try {
//            boolean alipayRSACheckedV2= AlipaySignature.rsaCheckV2(params, Configs.getAlipayPublicKey(),"utf-8", Configs.getSignType());
//            if(!alipayRSACheckedV2){
//                return ServerResponse.createByErrorMessage("非法请求,验证不通过");
//            }
//        } catch (AlipayApiException e) {
//            log.error("支付宝回调异常");
//        }
//        //todo 验证各种数据
//
//        ServerResponse serverResponse=iOrderService.alipayCallback(params);
//        if(serverResponse.isSuccess()){
//            return Const.AlipayCallback.RESPONSE_SUCCESS;
//        }
//        return Const.AlipayCallback.RESPONSE_FAILED;
//    }
//
//
//
//    @RequestMapping("query_order_status_pay.do")
//    @ResponseBody
//    public ServerResponse queryOrderPayStatus(HttpServletRequest httpServletRequest, Long orderNo){
//        String loginToken=CookieUtil.readLoginToken(httpServletRequest);
//        if(StringUtils.isEmpty(loginToken)){
//            return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
//        }
//        String userJsonStr= RedisShardedPoolUtil.get(loginToken);
//        User user=JsonUtil.String2Obj(userJsonStr,User.class);
//        if(user==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.ILLEGAL_ARGUEMENT.getDesc());
//        }
//        return iOrderService.queryOrderPayStatus(user.getId(),orderNo);
//    }




}
